'''
    处理1列数据的绘图模块
        1.绘图函数后加_layout为相应的风格设置函数
'''
import plotly.graph_objs as go

# 对一列数据绘制histogram
def column_histogram(df,col):
    x=list(df[col])
    trace=go.Histogram(x=x)
    return trace

def column_histogram_layout(col):
    layout = go.Layout(
        title=col,
        xaxis=dict(
            title='Value'
        ),
        yaxis=dict(
            title='Count'
        ),
    )
    return layout

# 对一列数据绘制pie
def column_pie(df,colname):
    tmp=df[colname].value_counts()
    labels=tmp.index
    values=tmp.values
    pie=go.Pie(labels=labels, values=values,
                    hoverinfo='label+percent', textinfo='value',
                    textfont=dict(size=20),
                    hole=.4,text=[colname],textposition='inside'
                )
    return pie

# 对一列数据绘制bar
def column_bar(df,col):
    x=df.columns.unique()
    y=[len(df[df[col]==lb]) for lb in x]
    trace=go.Bar(
            x=x,
            y=y
    )
    return trace

def column_bar_layout(col):
    layout = go.Layout(
        title=col,
        xaxis=dict(
            title='Value'
        ),
        yaxis=dict(
            title='Count'
        ),
    )
    return layout